package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hzdesk.media.koreabatchmatvector.adapter.FileDirListAdapter;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.logic.contract.FileSelectContract;
import com.hzdesk.media.koreabatchmatvector.utils.SDCardUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/10.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class FileSelectPersenter implements FileSelectContract.IFileSelectPersenter {
    private FileSelectContract.IFileSelectView mIFileSelectView ;
    private List<String> paths = null;
    private int file_type = AppConfig.FILE_TYPE_FILE_DIRE;
    private String rootPath = "/storage";
    private String curPath = "/";
    ListView fragmentMyFileSelectListview;
    Context mContext;
    private FileDirListAdapter adapter;
    public FileSelectPersenter(FileSelectContract.IFileSelectView mIFileSelectView) {
        this.mIFileSelectView = mIFileSelectView;
    }


    @Override
    public void initDate(ListView mListView, Context mcontext) {
        paths = new ArrayList<String>();
        fragmentMyFileSelectListview = mListView;
        mContext = mcontext;
        // 将容器paths的路径对应的文件夹和文件 显示文件名和图标
        adapter = new FileDirListAdapter(mContext, paths, file_type);
        fragmentMyFileSelectListview.setAdapter(adapter);
        getFileDir(rootPath);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                getFileDir(paths.get(i));
            }
        });
    }

    @Override
    public void onckilcedBack() {
        mIFileSelectView.toFinshPager();
    }

    @Override
    public void onClickedSelectPath() {
        getFileDir(new File(curPath).getParent());
    }

    //  获取该路径下的所有文件和文件夹的路径添加到 paths容器中 并控制fileSelectPath可不可以点击 --------------->这个fragment核心方法
    private void getFileDir(String filePath) {
        paths.clear();
        if (filePath.equals("/storage/emulated")) {
            filePath="/sdcard";
        }
        curPath = filePath;
        mIFileSelectView.showFileSelectPath(curPath);
        //    进度
        reFreshProfressBar(mContext);

        if (filePath.equals(rootPath)|| filePath.equals("/"))
            mIFileSelectView.setEnabled(false);
        else
            mIFileSelectView.setEnabled(true);

        //  获取该路径下的所有文件和文件夹的路径添加到 paths容器中
        File f = new File(filePath);
        File[] files = f.listFiles();

        // 判断当前下是否有文件夹
        if (files == null || files.length < 0) {
            return;

        }
        for (int i = 0; i < files.length; i++) {
            if (file_type == AppConfig.FILE_TYPE_FILE_DIRE) {
                // 过滤一遍
                // 1.是否为文件夹
                // 2.是否可访问
                if (files[i].isDirectory() && !files[i].getName().equals("self")) {
                    paths.add(files[i].getPath());
                }
            } else {
                if (files != null) {
                    paths.add(files[i].getPath());
                }
            }
        }
        adapter.notifyDataSetChanged();
        // fragmentMyFileSelectListview.setAdapter(adapter);
    }

    private void  reFreshProfressBar(Context mContext) {
        SDCardUtils sdCardUtils = new SDCardUtils(mContext, curPath);
        float a = sdCardUtils.getPercentage();
        int b = (int) (a * 100);
        mIFileSelectView.settotalProgress(b);
    }
}
